package com.lry.registy;


import com.alibaba.fastjson.JSONObject;
import com.lry.common.CommonConstants;
import com.lry.event.ProviderNodeWrapper;
import com.lry.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static com.lry.common.CommonConstants.*;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class URL implements Serializable {

    /**
     * 服务应用名称
     */
    private String applicationName;

    /**
     * 注册到节点到服务名称，例如：com.lry.test.UserService
     */
    private String serviceName;

    private String implName;

    private String host;

    private Integer port;

    private Map<String,Object> param;

    public void addParam(String key,Object value){
        if(null==param){
            param = new HashMap<>();
        }
        param.putIfAbsent(key,value);
    }

    public static String getParentProviderPath(String serviceName,String implName) {
        StringBuilder sb = new StringBuilder();
        sb.append(SLASH).append(ROOT).
                append(SLASH).append(PROVIDER).
                append(SLASH).append(serviceName);
//        if(StringUtils.isNotBlank(implName)){
//            sb.append(SLASH).append(implName);
//        }
        return sb.toString();
    }

    public static String[] getServiceNameFromParentProviderPath(String parProviderPath) {
        String[] names = new String[2];
        String[] items = parProviderPath.split(SLASH);
        names[0] = items[3];//interfaceName
        if(items.length==5){
            names[1] = items[4];//implName
        }
        return names;
    }

    public String getProviderPath() {
        StringBuilder sb = new StringBuilder();
        sb.append(SLASH).append(ROOT).
                append(SLASH).append(PROVIDER).
                append(SLASH).append(serviceName);

//        if(StringUtils.isNotBlank(implName)){
//            sb.append(SLASH).append(implName);
//        }
        sb.append(SLASH).append(host).append(COLON).append(port);
        return sb.toString();
    }

//    public String getProviderPath() {
//        StringBuilder sb = new StringBuilder();
//        sb.append(SLASH).append(ROOT).
//        append(SLASH).append(PROVIDER).
//        append(SLASH).append(serviceName).
//        append(SLASH).append(host).append(COLON).append(port);
//        return sb.toString();
//    }
    /**
     * 将URL转换为写入zk的provider节点下的一段字符串
     * @return
     */
    public String buildProviderNodeContent() {
        return JSONObject.toJSONString(this);
//        StringBuilder sb = new StringBuilder();
//
//        sb.append(applicationName).append(SEMI).
//                append(serviceName).append(SEMI);
//        if(StringUtils.isNotBlank(implName)){
//            sb.append(implName).append(SEMI);
//        }
//        sb.append(host).append(COLON).append(port).append(SEMI);
//
//        if(null!=param.get(WEIGHT)){
//            sb.append(WEIGHT).append(EQUAL).append(param.get(WEIGHT)).append(SEMI);
//        }
//        if(null!=param.get(VERSION)){
//            sb.append(VERSION).append(EQUAL).append(param.get(VERSION)).append(SEMI);
//        }
//        if(null!=param.get(GROUP)){
//            sb.append(GROUP).append(EQUAL).append(param.get(GROUP)).append(SEMI);
//        }
//        sb.append(System.currentTimeMillis());
//        return sb.toString();
    }

    public String getConsumerPath() {
        StringBuilder sb = new StringBuilder();
        sb.append(SLASH).append(ROOT).
                append(SLASH).append(CONSUMER).
                append(SLASH).append(applicationName).
                append(SLASH).append(serviceName);
        if(StringUtils.isNotBlank(implName)){
            sb.append(SLASH).append(implName);
        }
        sb.append(SLASH).append(host);
        return sb.toString();
    }

    /**
     * 将URL转换为写入zk的consumer节点下的一段字符串
     *
     * @return
     */
    public String buildConsumerNodeContent() {
        StringBuilder sb = new StringBuilder();
        sb.append(applicationName).append(SEMI).
                append(serviceName).append(SEMI).
                append(host).append(SEMI);
        if(null!=param.get(VERSION)){
            sb.append(VERSION).append(EQUAL).append(param.get(VERSION)).append(SEMI);
        }
        if(null!=param.get(GROUP)){
            sb.append(GROUP).append(EQUAL).append(param.get(GROUP)).append(SEMI);
        }
        sb.append(System.currentTimeMillis());
        return sb.toString();
    }


    /**
     * 将某个节点下的信息转换为一个Provider节点对象
     * applicationName;serviceName;host:port;weight=100;21312442142121
     * @param providerContent
     * @return
     */
//    public static ProviderNodeWrapper buildProviderNodeFromContent(String providerContent) {
//        String[] items = providerContent.split(SEMI);
//        ProviderNodeWrapper providerNodeInfo = new ProviderNodeWrapper();
//        providerNodeInfo.setServiceName(items[1]);
//
//        providerNodeInfo.setImplName(items[2]);
//
//        providerNodeInfo.setIp(items[2]);
//        String weightStr = items[3];
//        if(weightStr.startsWith(WEIGHT)){
//            providerNodeInfo.setWeight(Integer.valueOf(weightStr.split(EQUAL)[1]));
//        }
//        String versionStr = items[4];
//        if(versionStr.startsWith(VERSION)){
//            providerNodeInfo.setVersion(versionStr.split(EQUAL)[1]);
//        }
//        String groupStr = items[5];
//        if(groupStr.startsWith(GROUP)){
//            providerNodeInfo.setGroup(groupStr.split(EQUAL)[1]);
//        }
//        return providerNodeInfo;
//    }

    public static ProviderNodeWrapper buildProviderNodeFromContent(String providerContent) {
        URL url = JSONObject.parseObject(providerContent, URL.class);
        return ProviderNodeWrapper.builder().
                serviceName(url.getServiceName()).
                implName(url.getImplName()).
                ip(url.getHost()+COLON+url.getPort()).
                group((String) url.getParam().get(CommonConstants.GROUP)).
                version((String) url.getParam().get(CommonConstants.VERSION)).
                weight((Integer) url.getParam().get(CommonConstants.WEIGHT)).build();
    }

}
